$( document ).ready(function() {
/* ------------------- Tchat -----------------------------*/
	/*
	* The mouse leave the tchat area
	*/
	$("#tchatcontainer").mouseleave(function() {
		$("#tchatcontainer").height(30);
	});
	
	/*
	* The mouse enter the tchat area
	*/
	$("#tchatTab").mouseenter(function() {
		$("#tchatcontainer").height(200);
		$("#hiddablearea").fadeIn(200);
	});

	$("#hiddablearea").slideUp(500);
	$("#tchatcontainer").height(30);

});

app.controller('admincontroller', ['$scope', '$parse', '$http', '$rootScope', function($scope, $parse, $http, $rootScope)
{
	//@todo : If user isn't admin then don't load  getEventSubscriptionsToValidCommand...

	$scope.isComponentLoading = true;
	$scope.servicebase = '../../Core/server.class.php';
	
	// listen for the event in the relevant $scope
	$scope.$on('currentUser', function (event, data) {
		$rootScope.currentUser = data.user;
		$scope.editUserHasAvatar = ($rootScope.currentUser.Avatar == '') ? false : true;
		$scope.getPersonalEventsCommand();
	});
	
	$scope.$on('translations', function(event, args) {
		$scope.translations = args.translations.response;
		$scope.getAllEventSubscriptionStatesCommand();
		$scope.getEventSubscriptionsToValidCommand();
	});
	
	/**
	* Get personal events
	*/ 
	$scope.getPersonalEventsCommand = function()
	{
		console.info("getPersonalEventsCommand - called");
		$scope.personalevents = [];
		
		$http.post($scope.servicebase, { "service" : "getPersonalEvents", "userId" : $rootScope.currentUser.Id}).
		success(function(data, status) 
		{
			console.info("getPersonalEventsCommand - call success"); 
			$scope.data = data;
			$scope.result = data; 
			if(data.isFailed == true)
			{
				$scope.setMessageCommand($scope.data.exception, 'danger');
			}
			
			$scope.personalevents = $scope.data.response;
		})
		.
		error(function(data, status) 
		{
			console.error("getPersonalEventsCommand - call failed"); 	
			$scope.loadingEvents = false;
			$scope.data = data || "Request failed";	
			console.error("getPersonalEventsCommand - data : " + $scope.data); 				
		});
	}
	
	/**
	*	Get all active tasks
	*/
	$scope.getTasksCommand = function()
	{
		console.info("getTasksCommand - called"); 
		$scope.loadingTasks = true;
		
		$http.post($scope.servicebase, { "service" : "getTasks", "user" : $rootScope.currentUser.Id }).
		success(function(data, status) 
		{
			console.info("getTasksCommand - call success"); 
			$scope.status = status;
			$scope.data = data;
			$scope.result = data; 
			$scope.loadingTasks = false;
			if(data.isFailed == true)
			{
				$scope.setMessageCommand($scope.data.exception, 'danger');
			}
		
			$scope.tasks = $scope.data.response;
		})
		.
		error(function(data, status) 
		{
			console.error("getUsersCommand - call failed"); 	
			$scope.loadingTasks = false;
			$scope.data = data || "Request failed";
			$scope.status = status;			
			console.error("getUsersCommand - data : " + $scope.data); 				
		});
	};
	
	//@todo : Get user events
	
	/**
	* Get all events subscriptions
	*/
	$scope.getEventSubscriptionsToValidCommand = function()
	{
		console.info("getEventSubscriptionsToValidCommand - called"); 
		$scope.loadingEventSubscriptions = true;
		
		$scope.eventsubscriptions = [];
		
		$http.post($scope.servicebase, { "service" : "getEventSubscriptionsToValid"}).
		success(function(data, status) 
		{
			console.info("getEventSubscriptionsToValidCommand - call success"); 
			$scope.status = status;
			$scope.data = data;
			$scope.result = data; 
			$scope.loadingEventSubscriptions = false;
			if(data.isFailed == true)
			{
				$scope.setMessageCommand($scope.data.exception, 'danger');
			}
			else
			{
				$scope.eventsubscriptions = $scope.data.response;
				
				for (i = 0; i < $scope.eventsubscriptions.length; i++)
				{
				   var eventsubscription = $scope.eventsubscriptions[i];
				   eventsubscription.State = $scope.eventSubscriptionsStates[eventsubscription.State];
				} 
			}
		})
		.
		error(function(data, status) 
		{
			console.error("getEventSubscriptionsToValidCommand - call failed"); 	
			$scope.loadingEventSubscriptions = false;
			$scope.data = data || "Request failed";
			$scope.status = status;			
			console.error("getEventSubscriptionsToValidCommand - data : " + $scope.data); 				
		});
	};
	
	/**
	* Get all states event subscription
	*/
	$scope.getAllEventSubscriptionStatesCommand = function()
	{
		console.info("getAllEventSubscriptionStatesCommand - called");
		
		$scope.eventSubscriptionsStates = [];
		
		$http.post($scope.servicebase, { "service" : "getEventSubscriptionStates"}).
		success(function(data, status) 
		{
			console.info("getEventSubscriptionStates - call success"); 
			$scope.status = status;
			$scope.data = data;
			$scope.result = data;
			if($scope.data.isFailed == true)
			{
				$scope.setMessageCommand($scope.data.exception, 'danger');
			}
			else
			{
				for(var state in $scope.data.response) {
				   $scope.eventSubscriptionsStates.push($scope.translations["EventSubscriptions_State_" + state]);
				}
			}
		})
		.
		error(function(data, status) 
		{
			console.error("getEventSubscriptionStates - call failed"); 	
			$scope.data = data || "Request failed";
			$scope.status = status;
			console.error("getEventSubscriptionStates - data : " + $scope.data);		
		});
	};
	
	/**
	* Check new tchat messages
	*/
	$scope.checkNewTchatMessagesCommand = function()
	{
		if($rootScope.getCurrentUser() != undefined)
			{
				//Check user authentification
				$http.post($scope.servicebase, { "service" : "checkUserAuthorization", "userid" : $rootScope.currentUser.Id}).
				success(function(data, status) 
				{			
					//console.info("checkUserAuthorization - call success");
				})
				.
				error(function(data, status) 
				{
					$scope.setMessageCommand('checkUserAuthorization failed', 'danger');
				});

				//Check if there is new tchat messages
				$http.post($scope.servicebase, { "service" : "getTchatMessages"}).
				success(function(data, status) 
				{
					if(data.isFailed == false)
					{
						for (i = 0; i < data.response.length; i++)
						{
							var dalTchatMessage = data.response[i];
							//console.info(JSON.stringify(dalTchatMessage));
							if($.inArray(dalTchatMessage.Id, $scope.tchatMessagesIds) == -1)
							{
								var tchatMessage = new TchatMessage(dalTchatMessage);
								console.info(JSON.stringify(tchatMessage));
								Array.prototype.unshift.call($scope.tchatMessages, tchatMessage);
								$scope.tchatMessagesIds.push(dalTchatMessage.Id);
							}
						} 
					}
				})
				.
				error(function(data, status) 
				{
					$scope.setMessageCommand('Serveur de tchat KO', 'danger');
				});
			}
	};
	
	/**
	* Get all online users
	*/
	$scope.getOnlineUsersCommand = function() {
		$http.post($scope.servicebase, { "service" : "getOnlineUsers"}).
		success(function(data, status) 
		{			
			console.log("admin.getOnlineUsers - call success");
			
			if(data.isFailed == false)
			{	
				console.log('admin.getOnlineUsers - ' + data.response.length + ' connected user');
			}
			else
			{
				console.error('admin.getOnlineUsers - ' + data.exception);
			}
		})
		.
		error(function(data, status) 
		{
			$scope.setMessageCommand('checkUserAuthorization failed', 'danger');
		});
	};
	
	/**
	*	Load the admin component
	*/
	$scope.loadAdminComponentCommand = function() 
	{
		console.info("loadAdminComponentCommand - called");
		
		console.info("Set variables");
		//$scope.tasks = [];
		$scope.eventSubscriptionsStates = [];
		$scope.tchatMessages = [];
		$scope.tchatMessagesIds = [];
		$scope.lastTchatMessageId = 0;
		$scope.isMessageSending = false;
		$scope.isComponentLoading = false;
	};
	
	$scope.loadAdminComponentCommand();
	
	/**
	* Check if server is available
	*/
	setInterval(function()
	{
		$scope.getOnlineUsersCommand();
	}, 3000);
	
	/**
	* Check if server is available
	*/
	setInterval(function()
	{
		$scope.checkNewTchatMessagesCommand();
	}, 2500);
}]);